package com.xr.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xr.constant.MessageConstant;
import com.xr.pojo.Menu;
import com.xr.result.Result;
import com.xr.service.MenuService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("/menus")
public class MenuController {

    @Reference
    private MenuService menuService;

    @RequestMapping("/getMenus")
    public Result getUserName(){
        try{
            // 获取登陆用户的认证信息
            User loginUser = (User)
                    SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            System.out.println("left -- join "+loginUser);
            // 登陆用户名
            String username = loginUser.getUsername();
            System.out.println("left -- join "+username);

            List<Menu> menu = menuService.findUserName(username);
            // 返回给前端
            return new Result(true,
                    MessageConstant.GET_USERNAME_SUCCESS,menu);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.GET_USERNAME_FAIL);
    }
}
